﻿using System;
using System.Collections.Generic;
using System.Text;
using System.Windows.Forms;
using System.Drawing;

namespace VirtualControl
{
    public class VirtualHScroll : VirtualBase
    {
        public event EventHandler Scroll;
        public float MaxValue = 100f;
        public float MinValue = 0f;
        float value;
        public float Value
        {
            get { return this.value; }
            set
            {
                this.value = value;
                Invalidate();
            }
        }
        
        public VirtualHScroll(Control parent)
            : base(parent)
        {
        }

        protected override void OnMouseDown(MouseEventArgs e)
        {
            this.Value = e.X * (this.MaxValue - this.MinValue) / this.Bounds.Width + this.MinValue;
            if (this.Scroll != null) this.Scroll(this, null);
            base.OnMouseDown(e);
        }

        protected override void OnMouseMove(MouseEventArgs e)
        {
            if (this.isMouseDown)
            {
                this.Value = e.X * (this.MaxValue - this.MinValue) / this.Bounds.Width + this.MinValue;
                if (this.Scroll != null) this.Scroll(this, null);
            }
            base.OnMouseMove(e);
        }

        protected override void OnPaint(System.Drawing.Graphics G)
        {
            G.DrawLine(Pens.Lime, this.Bounds.Left, this.Bounds.Top + this.Bounds.Height / 2, this.Bounds.Right - 1, this.Bounds.Top + this.Bounds.Height / 2);
            G.FillEllipse(Brushes.YellowGreen, this.Bounds.Left + (this.Value - this.MinValue) * this.Bounds.Width / (this.MaxValue - this.MinValue) - 10, this.Bounds.Top + (this.Bounds.Height - 10) / 2, 10, 10);

            base.OnPaint(G);
        }
    }
}
